package com.hzau.tourist_service_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzau.tourist_service_sys.dao.EmergencyDao;
import com.hzau.tourist_service_sys.pojo.Emergency;
import com.hzau.tourist_service_sys.result.Result;
import com.hzau.tourist_service_sys.service.EmergencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDate;

@Transactional
@Service
public class EmergencySericeImpl implements EmergencyService
{
    int pagesize=10;
    @Autowired
    EmergencyDao emergencyDao;
    @Override
    public Result save(Emergency emergency) {
        if (emergency.getECreateTime()==null){
            emergency.setECreateTime(new Timestamp(System.currentTimeMillis()));
        }

        emergency.setEStatus(0);
        emergencyDao.insert(emergency);
        return Result.success();
    }

    @Override
    public Result updateSatus(long emergencyId) {
        Emergency emergency=new Emergency();
        emergency.setEmergencyId(emergencyId);
        emergency.setEStatus(1);
        emergencyDao.updateById(emergency);
        return Result.success();
    }

    @Override
    public Result updateInfo(Emergency emergency) {
        emergencyDao.updateById(emergency);
        return Result.success();
    }

    @Override
    public Result deleteById(Long emergencyId) {
        emergencyDao.deleteById(emergencyId);
        return Result.success();
    }

    @Override

    public Result findPage(Emergency emergency) {
        LambdaQueryWrapper<Emergency> lqw=new LambdaQueryWrapper<>();
        if(emergency.getPagenum()==null){emergency.setPagenum(0);}
        lqw.le(emergency.getEEndTime()!=null,Emergency::getEEndTime,emergency.getEEndTime());
        lqw.eq(emergency.getEStatus()!=null,Emergency::getEStatus,emergency.getEStatus());
        lqw.in(emergency.getTypes().size()!=0,Emergency::getEType,emergency.getTypes());
        lqw.eq(emergency.getELevel()!=null,Emergency::getELevel,emergency.getELevel());
        lqw.orderByDesc(Emergency::getEmergencyId);
        IPage page=new Page(emergency.getPagenum(),pagesize);
        emergencyDao.selectPage(page,lqw);
        return Result.success(page.getRecords());
    }

}
